package com.test.fundible.fundible_06_loan_apply;

import static com.test.fundible.fundible_00_utils.FundibleUtil.startLoading;
import static com.test.fundible.fundible_00_utils.FundibleUtil.stopLoading;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.test.fundible.R;
import com.test.fundible.fundible_00_customer_service.Customer_fundibleServiceDialog;
import com.test.fundible.fundible_00_utils.Loading_fundibleDialog;
import com.test.fundible.fundible_04_home.MainAct_fundibleivity;
import com.test.fundible.fundible_00_http.OkHttpUtil;

import java.util.ArrayList;

public class LoanApplyActivity extends AppCompatActivity {

    private TextView loanAmountTv,loanTermTv;
    private LinearLayout itemGroupLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fundible_loan_aply);
        //tag2024
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        SwipeRefreshLayout refreshLayout = findViewById(R.id.afla_refreshlayout);
        refreshLayout.setOnRefreshListener(() -> {
            refreshLayout.setRefreshing(false);
            startLoading(this,"");
            OkHttpUtil.getInstance_fundible().fetchLnAplDetail(this::onAplyDetailResp_fundible);
        });

        findViewById(R.id.afla_ib_back).setOnClickListener(v -> onBackPressed());
        findViewById(R.id.afla_ib_customerservice).setOnClickListener(v -> {
            OkHttpUtil.getInstance_fundible().uploadEventPoint("APPLY","CUSTOMER_SERVICE_CLICK","");
            new Customer_fundibleServiceDialog(this).show();
        });

        loanAmountTv = findViewById(R.id.afla_tv_loanamount);
        loanTermTv = findViewById(R.id.afla_tv_loanterm);
        itemGroupLayout = findViewById(R.id.afla_layout_itemgroup);


        findViewById(R.id.afla_btn_submit).setOnClickListener(v -> {
            startLoading(this,"");
            OkHttpUtil.getInstance_fundible().uploadEventPoint("APPLY","SUBMIT_CLICK","");
            OkHttpUtil.getInstance_fundible().aplyLn(this::onLnAplyResp_fundible);
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        startLoading(this,"");
        OkHttpUtil.getInstance_fundible().uploadEventPoint("APPLY","START","");
        OkHttpUtil.getInstance_fundible().fetchLnAplDetail(this::onAplyDetailResp_fundible);
    }

    @SuppressLint("SetTextI18n")
    public void onAplyDetailResp_fundible(){

        stopLoading();

        String amount = LoanApplyDetail.getInstance_fundible().getAmount_fundible();
        String currency = LoanApplyDetail.getInstance_fundible().getCurrency_fundible();

        loanAmountTv.setText(amount+" "+currency);
        loanTermTv.setText(LoanApplyDetail.getInstance_fundible().getTermStr_fundible());

        refreshParamView_fundible();
    }

    @SuppressLint("InflateParams")
    private void refreshParamView_fundible(){

        itemGroupLayout.removeAllViews();

        ArrayList<LnoaAp_fundiblel_fundibleyItem> params = LoanApplyDetail.getInstance_fundible().getDetailItemList_fundible();
        View groupView = null;
        LinearLayout groupLayout = null;

        for (LnoaAp_fundiblel_fundibleyItem lnAplyItem:params){

            if (lnAplyItem.isTitle_fundible()){
                if (groupView!=null && groupLayout!=null) itemGroupLayout.addView(groupView);
                groupView = LayoutInflater.from(this).inflate(R.layout.view_fundible_item_bg_fundible_group,null);
                groupLayout = groupView.findViewById(R.id.vibg_layout_group);
                addItem(groupLayout,lnAplyItem);
            }else {
                if (groupView!=null && groupLayout!=null) addItem(groupLayout,lnAplyItem);
            }

        }

        itemGroupLayout.addView(groupView);

    }

    private void addItem(LinearLayout group, LnoaAp_fundiblel_fundibleyItem lnAplyItem){

        @SuppressLint("InflateParams") View item = LayoutInflater.from(this).inflate(R.layout.item_fundible_lnaply_detail,null);

        TextView nameTv = item.findViewById(R.id.ilda_tv_name);
        TextView valueTv = item.findViewById(R.id.ilda_tv_value);

        nameTv.setText(lnAplyItem.getShowName_fundible());
        valueTv.setText(lnAplyItem.getShowDetail_fundible());

        if (lnAplyItem.isTitle_fundible()){
            nameTv.setTextColor(Color.parseColor("#FF212321"));
            nameTv.setTypeface(Typeface.DEFAULT_BOLD);
            valueTv.setTextColor(Color.parseColor("#FF1E492E"));
            valueTv.setTypeface(Typeface.DEFAULT_BOLD);
        }

        group.addView(item);
    }

    public void onLnAplyResp_fundible(){
        OkHttpUtil.getInstance_fundible().requestUsPrcs(this::onProcessResp_fundible);
    }

    public void onProcessResp_fundible(){
        stopLoading();
        Intent intent = new Intent(this, MainAct_fundibleivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        OkHttpUtil.getInstance_fundible().uploadEventPoint("APPLY","END","");
    }

}